package com.longbig.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author: yyl
 * @date: 2025/4/14:20:41
 */
@Data
public class X6Node {
    private String id;
    private NodeTypeEnum type;
    private String label;
    private String parentId;
    private List<X6Edge> outgoingEdges = new ArrayList<>();
    // 新增：子节点集合（用于处理嵌套关系）
    private List<X6Node> children = new ArrayList<>();

    public X6Node addOutgoingEdge(X6Edge edge) {
        // 1. 空值校验
        if (edge == null) {
            throw new NullPointerException("Edge cannot be null");
        }

        // 2. 源节点ID校验
        if (!edge.getSourceId().equals(this.id)) {
            throw new IllegalArgumentException(
                    String.format("Edge sourceId[%s]与当前节点ID[%s]不匹配",
                            edge.getSourceId(), this.id)
            );
        }

        // 3. 防止重复添加
        if (!outgoingEdges.contains(edge)) {
            outgoingEdges.add(edge);
        }

        return this; // 支持链式调用
    }


    // 新增：获取目标节点的边
    public Optional<X6Edge> getEdgeTo(String targetId) {
        return outgoingEdges.stream()
                .filter(e -> e.getTargetId().equals(targetId))
                .findFirst();
    }

    // Getter/Setter
    public List<X6Edge> getOutgoingEdges() {
        return Collections.unmodifiableList(outgoingEdges);
    }

    public List<X6Node> getChildren() {
        return children;
    }
}
